package com.itheima.web.controller.sms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.service.sms.AdvertiseService;
import com.itheima.utils.PageResult;
import com.itheima.utils.PageUtils;
import com.itheima.utils.ResultData;
import com.itheima.vo.sms.Advertise;
import com.itheima.vo.sms.AdvertiseVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/home/advertise")

public class AdvertiseController {
    @DubboReference
    private AdvertiseService advertiseService;


    /**
     * 添加广告
     * @param advertise
     * @return
     */
    @PostMapping("/create")
    public ResultData create(@RequestBody Advertise advertise){
        advertiseService.save(advertise);
        return ResultData.ok("添加成功",null);
    }

    /**
     * 根据id删除广告
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public ResultData delete(@RequestParam List ids){
        advertiseService.removeByIds(ids);
        return ResultData.ok("删除成功",null);
    }

    /**
     * 分页查询
     * @param endTime
     * @param name
     * @param pageNum
     * @param pageSize
     * @param type
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String endTime, String name, Integer pageNum, Integer pageSize, Integer type) {
        Page<Advertise> pageInfo = advertiseService.getPage(endTime,name,pageNum,pageSize,type);
        PageResult page = PageResult.getPage(pageInfo);

        return ResultData.ok("查询成功",page);
    }

    /**
     * 修改上下线状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/update/status/{id}")
    public ResultData updateStatus(@PathVariable Long id, Integer status){
        Advertise advertise = advertiseService.getById(id);

        advertise.setStatus(status);

        advertiseService.updateById(advertise);
        return ResultData.ok("修改状态成功",null);
    }

    /**
     * 修改广告
     * @param advertise
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public ResultData updateById(@RequestBody Advertise advertise,@PathVariable Long id){
        advertiseService.updateById(advertise);
        return ResultData.ok("修改成功",null);
    }

    /**
     * 获取广告详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultData getById(@PathVariable Long id){
        Advertise advertise=advertiseService.getById(id);
        return ResultData.ok("查询成功",advertise);
    }
}
